package com.test.demo;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myprojectoffeixue.R;
import com.macrovideo.custom.RoundCornerImageView;
import com.macrovideo.objects.PTZXPoint;
import com.macrovideo.sdk.defines.Defines;
import com.macrovideo.sdk.defines.ResultCode;
import com.macrovideo.sdk.media.LibContext;
import com.macrovideo.sdk.media.LoginHandle;
import com.macrovideo.sdk.media.NVMediaPlayer;
import com.macrovideo.sdk.media.Player;
import com.macrovideo.sdk.tools.Functions;

@SuppressLint("NewApi")
public class NVPlayerPlayActivity extends Activity implements OnClickListener, OnTouchListener, OnItemClickListener{
	static final short SHOWCODE_LOADING=1001;//���ڼ���
	static final short SHOWCODE_NEW_IMAGE=1002;//��ͼƬ
//	static final short SHOWCODE_NOTICE=1003;//��ʾ
	static final short SHOWCODE_VIDEO=1004;//��ʾ
	static final short SHOWCODE_STOP=2001;//ֹͣ����
	static final short SHOWCODE_HAS_DATA=3001;//������
	static final short STAT_CONNECTING=2001;//�������ӷ�����
	static final short STAT_LOADING=2002;//���ڼ�����Ƶ
	static final short STAT_DECODE=2003;//����
	static final short STAT_STOP=2004;//ֹͣ
	static final short STAT_DISCONNECT=2005;//���ӶϿ�
	static final short STAT_RESTART=2006;//��������
	static final short STAT_MR_BUSY=2007;//��������
	static final short STAT_MR_DISCONNECT=2008;//��������9
	private int m_nID=0;
	private int m_nDeviceID=0;
	private String m_strName="IPC";
	private int m_nAddType=0;
	private String m_strUsername="1";
	private String m_strPassword="1";
	private boolean m_bSpeak = false;
	private boolean m_bPTZ = false;
	private boolean m_bPTZX = false;
	private int m_nPTZXCount = 0;
	private boolean m_bReversePRI=true;
	private int mPlayingChn=-1;
	private boolean mPlaySound=false;
	private Button mBtnStopAndPlay;
	private Button mBtnSound;
	private Button mBtnSound2;  // add by mai 2015-3-25
 	private LinearLayout layoutTopBar=null;
 	private boolean mIsPlaying=false;
	private LinearLayout layoutBottomBar;
	private LinearLayout layoutCenter=null;
	private LinearLayout LayoutMicroPhoneBottom=null; //layoutMicroPhone=null,
	private boolean bIsLeftPressed=false, bIsRightPressed=false, bIsUpPressed=false, bIsDownPressed=false;

 	//==add by mai 2015-1-20=========
	private ImageView mBtnBack;
	private Button mBtnBack2;  // add by mai 2015-3-25
	//===end add by mai 2015-1-20====

  	private boolean mIsSpeaking=false;
 	
	private boolean m_bFinish = false;
	int mScreenWidth = 0;//��Ļ��   
	int mScreenHeight = 0;//��Ļ�� 
 
	private boolean mQLHD=true;//�Ƿ�֧�ָ���
	private int mStreamType=0;//��ǰ���ŵ�����
	private LinearLayout layoutImageQL;//���๤����
	private LinearLayout mPTZPanel;//���๤����
	private RadioButton mBtnHD=null, mBtnSmooth=null;
	private Button btnHD=null, btnSmooth=null;
	private Button  mBtnImageQl=null, mBtnMic=null, mBtnReverse=null, mBtnScreenShot=null;
	private Button  mBtnImageQl2=null, mBtnMic2=null, mBtnReverse2=null, mBtnScreenShot2=null;  // add by mai 2015-3-25
	private RadioButton mBtnVerticalAuto=null, mBtnHorizontalAuto=null;
	private LinearLayout llLandscapeDefinition, llVertical, llLandscape; // add by mai 2015-3-24
	private RadioGroup rgLandscapeDefinition;  // add by mai 2015-3-25
	NVMediaPlayer[] glViews = {null,null,null,null};
	LinearLayout[] containers = {null,null,null,null};
	private View top_bottom[] = new View[2];
	private View parentContainers[] = new View[Player.PLAYER_NUM];
	private View backgroundContainers[] = new View[Player.PLAYER_NUM];
	private ImageView img_v[] = new ImageView[Player.PLAYER_NUM];
	private int nScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
	private GestureDetector mGestureDetector=null;
	private ScaleGestureDetector mScaleGestureDetector = null;
	private boolean mIsReverse=false;//add  by luo 20141124
	//add by luo 20141217
	private Dialog iamgeViewDialog=null;
	private View iamgeViewConctentView=null;
 	private Button btnCancelImageView=null;
 	//end add by luo 20141217
	private TextView tvPlayDeviceID; // add by mai 2015-3-13
	private LinearLayout llPlayTalkback; //add by mai 2015-3-14
	private RelativeLayout controlPanel; //add by mai 2015-3-14
	private Button btnPTZLeft, btnPTZRight, btnPTZUP, btnPTZDown; //add by mai 2015-3-14
	private boolean bAnyway = true;
	private Dialog screenshotDialog=null;
	private int FLING_MIN_DISTANCE = 10;  
    private int FLING_MIN_VELOCITY = 80;  
    private int FLING_MAX_DISTANCE = FLING_MIN_DISTANCE;
    private static int BTN_SCREENSHOT = 10010; // add by mai 2015-6-25 �ӳٸ��½�ͼ
    private ListView lvPreset; // add by mai 2015-7-30 Ԥ��λ��
    private PopupWindow popupWindowMore; // add by mai 2015-7-30
    private ImageView btnPresetConfig; // add by mai 2015-7-30
    private PTZXPiontAdapter ptzxAdapter = null;//add by luo 20150803
    private static final int PTZX_RESULT_OK = 1111;  //add by mai 2015-7-31 Ԥ��λ���û���ʹ��
    private static final int PTZX_RESULT_FAIL = 1112;  //add by mai 2015-7-31 Ԥ��λ���û���ʹ��
    private ProgressBar progressBarPresetConfig; //add by mai 2015-7-31 ״̬ת��
    private ImageView ivPresetLandscape; // add by mai 2015-7-31 ����ʱ���Ԥ��λ��ť
    //add by mai 20151210
    private LoginHandle deviceParam = null; //��¼����ʱ���õ�
    //end add by mai 20121210
	private void createDialogs() { // ������½ʱ ���������Ի���
		//add by luo 20141226
		iamgeViewConctentView = LayoutInflater.from(this).inflate(R.layout.screenshotdialog, null);
		screenshotDialog= new Dialog(this, R.style.progressDialog);  
		screenshotDialog.setContentView(iamgeViewConctentView);
		screenshotDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			@Override
			public void onShow(DialogInterface dialog){

				 Message msg=handler.obtainMessage();
				 msg.arg1 = Defines.MSG_SCREENSHOT;
				 handler.sendMessage(msg);
			}
					
		});
		screenshotDialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
			@Override
			public void onDismiss(DialogInterface dialog) {

			} 
					
		});
		 
	}
	    
	   
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// ����Ƿ��ؼ�,ֱ�ӷ��ص�����
		if(keyCode == KeyEvent.KEYCODE_BACK ){

			if(mIsPlaying==false){
				  stopPlay(false); //ֹͣ����
 			      Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
			      m_bFinish = true;
			      startActivity(intent);
				  NVPlayerPlayActivity.this.finish();  
				  overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
				  return false;
			  }
			
			new AlertDialog.Builder(NVPlayerPlayActivity.this)
			.setTitle(getString(R.string.alert_stop_play))
			.setIcon(R.drawable.icon)
			.setNegativeButton(getString(R.string.alert_btn_NO), null)
			.setPositiveButton(getString(R.string.alert_btn_YES), new DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int whichButton) {

				setResult(RESULT_OK);
				stopPlay(false);
				LibContext.ClearContext();
				ReleaseGLViewPlayer();
 			  
 			  
				Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
				m_bFinish = true;
		     
				startActivity(intent);
		      
				LocalDefines.B_UPDATE_LISTVIEW = true; // add by mai 2015-6-29 ˢ�½���
				NVPlayerPlayActivity.this.finish();  
			  
				overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
 				}

			}).show();
		}
		
		return false;
	}
	
	
	//��ȡ�Ƿ񲥷���Ƶ������
	private void readSystemParam(){
   	 
        SharedPreferences ap =  getSharedPreferences(Defines._fileName, MODE_PRIVATE); 
        mPlaySound = ap.getBoolean("sounds", true);
    
    }
    
	//������Ƶ�Ƿ�ر�
	public  boolean writeSystemParam(){
		SharedPreferences ap =  getSharedPreferences(Defines._fileName, MODE_PRIVATE);
		SharedPreferences.Editor editer = ap.edit();
		editer.putBoolean("sounds", mPlaySound);
		editer.commit();
		
		return true; 
	}
	//��ʾ����ʾ
	//msg����ʾ����Ϣ
	private void ShowAlert(String title, String msg){
		try{
			new AlertDialog.Builder(NVPlayerPlayActivity.this)
			.setTitle(title).setMessage(msg)
			//.setTitle(getString(R.string.Alert_Title)).setMessage(msg)
			.setIcon(R.drawable.icon)
			.setPositiveButton(getString(R.string.alert_btn_OK), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {

			  setResult(RESULT_OK);
			}
			}).show();

		}catch(Exception e){
			
		}
			
		}
		
	@Override
	public void onPause(){
		OnPlayersPuase();
		super.onPause();
	}
	
	@Override
	public void onResume(){
		
		OnPlayersResume(); //���¿�ʼ����
		nToolsViewShowTickCount = 8; 
		timerThreadID++;
		new TimerThread(timerThreadID).start();
    	if(mIsPlaying){    		
    		startPlay(); //��ʼ����
    	}else{
    		stopPlay(true);  //ֹͣ����
    	}
		m_bFinish = false;
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);		
        notiManager.cancel(LocalDefines.notificationID);
        nScreenOrientation =this.getResources().getConfiguration().orientation;
        super.onResume();
	}
	

	//���¿�ʼ����
	public void OnPlayersResume()
    {
    	int i = 0;
    	for(i = 0;i < Player.PLAYER_NUM;i++)
    	{
    		if(glViews[i]!=null){
    			glViews[i].onResume();
    		}    		 
    	}
    }
	@Override
	public void onDestroy(){ //����ʱ����

		glViews = null;
		containers = null;
		top_bottom = null;
		parentContainers = null;
		backgroundContainers = null;
		img_v = null;
		
		//add by mai 2015-7-9 ��ձ���ͼƬ
		BitmapDrawable llVerticals = (BitmapDrawable) llVertical.getBackground();
		llVertical.setBackgroundResource(0); // ������Ϊ��
		llVerticals.setCallback(null);
		llVerticals.getBitmap().recycle();
		
		BitmapDrawable llLandscapeDefinitions = (BitmapDrawable) llLandscapeDefinition.getBackground();
		llLandscapeDefinition.setBackgroundResource(0); // ������Ϊ��
		llLandscapeDefinitions.setCallback(null);
		llLandscapeDefinitions.getBitmap().recycle();
		
		BitmapDrawable btnPTZLefts = (BitmapDrawable) btnPTZLeft.getBackground();
		btnPTZLeft.setBackgroundResource(0); // ������Ϊ��
		btnPTZLefts.setCallback(null);
		btnPTZLefts.getBitmap().recycle();
		
		BitmapDrawable btnPTZRights = (BitmapDrawable) btnPTZRight.getBackground();
		btnPTZRight.setBackgroundResource(0); // ������Ϊ��
		btnPTZRights.setCallback(null);
		btnPTZRights.getBitmap().recycle();
		
		BitmapDrawable btnPTZUPs = (BitmapDrawable) btnPTZUP.getBackground();
		btnPTZUP.setBackgroundResource(0); // ������Ϊ��
		btnPTZUPs.setCallback(null);
		btnPTZUPs.getBitmap().recycle();
		
		BitmapDrawable btnPTZDowns = (BitmapDrawable) btnPTZDown.getBackground();
		btnPTZDown.setBackgroundResource(0); // ������Ϊ��
		btnPTZDowns.setCallback(null);
		btnPTZDowns.getBitmap().recycle();
		//end add by mai 2015-7-9 
		super.onDestroy();
	}
	
	
	@SuppressWarnings("deprecation")
	@Override
	public void onStop(){
		 
		timerThreadID++;
		m_nThreadID++; //�ӳٸ��½�ͼ�߳�ʹ��
 		if(!m_bFinish){//����ǰ�����home�����µ�ֹͣ���ͱ��浱�ڵ�����
  
		NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);			
		Notification notification =null;
		int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
	
		if (currentapiVersion > 11)
		{
			notification =new Notification.Builder(getApplicationContext())
		         .setContentTitle(getString(R.string.app_name))
		         .setContentText(getString(R.string.app_name))
		         .setSmallIcon(R.drawable.icon)
		         .build();
		}else{
				
			notification = new Notification(R.drawable.icon, getString(R.string.app_name), System.currentTimeMillis());	
		}
			
		notification.flags = Notification.FLAG_NO_CLEAR;		
		notification.flags |=  Notification.FLAG_ONGOING_EVENT;
			
		Intent intent = new Intent(this, NVPlayerPlayActivity.class);				
		Bundle data = new Bundle();
 			//�����û�������Ϣ
		data.putString("name", m_strName); 
		if(mIsPlaying){//����
			data.putBoolean("isplaying", true);//���ͨ����
			data.putInt("playing_chn", mPlayingChn);//���ͨ����
		}
		data.putParcelable("", deviceParam);
 		intent.putExtras(data);
 		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);		
		PendingIntent contentIntent = PendingIntent.getActivity(this, LocalDefines.notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);		
		//notification.setLatestEventInfo(NVPlayerPlayActivity.this, getString(R.string.app_name), getString(R.string.app_notice),contentIntent);		
			
		notiManager.notify(LocalDefines.notificationID, notification);
		LibContext.stopAll();//add by luo 20141219
 	
		}else{
			
			LibContext.stopAll();//add by luo 20141219
			LibContext.ClearContext();
		}
 
		m_bFinish = true;
		super.onStop();
	}
    	
	//����
	private void ShowLandscapeView(){
		
		synchronized(this)
		{
			bAnyway = false; //add by mai 2015-3-23
			nToolsViewShowTickCount=5;
 				
		    int nWidth = mScreenWidth;
   	        int nHeight = mScreenHeight;
   	        double dWidth = nHeight*1.7777777;
   	        if(dWidth<nWidth)nWidth=(int) dWidth;

   	        if(controlPanel!=null)
   	        {
   	        	controlPanel.setVisibility(View.GONE);
   	        }
   	  
   	        if(popupWindowMore !=null)
   	        {
   	        	popupWindowMore.dismiss();
   	        }
   	        
   	        hideToolsViews();
   	     
   	        if(layoutCenter!=null){
   	        	RelativeLayout.LayoutParams rlp=new RelativeLayout.LayoutParams(nWidth, nHeight);
   	        	rlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
   	        	layoutCenter.setLayoutParams(rlp);
   	        	layoutCenter.setPadding(0, 0, 0, 0);
   	        }
   	  
   	        nScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
   	        for(int i = 0;i < Player.PLAYER_NUM;i++)
   	        {
   	        	if(glViews[i]!=null){
   	        		glViews[i].onOreintationChange(nScreenOrientation);
   	        	}	 
	    		
   	        }
		}
		
	}
	
	//����
	private void ShowPortrailView(){
		synchronized(this)
		{
			if(m_bPTZX)
			{
				btnPresetConfig.setVisibility(View.VISIBLE);
			}else{
				btnPresetConfig.setVisibility(View.INVISIBLE);
			}
			 
			if(mScreenWidth > mScreenHeight)
			{
				ShowLandscapeView();
			}else{
			//dipת��Ϊpx
 			int padding_in_dp = 45;  // 6 dps
 		    final float scale = getResources().getDisplayMetrics().density;
 		    int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
 		    bAnyway = true; //add by mai 2015-3-23
			showToolsViews();
 
			int nWidth = mScreenWidth;
			int nHeight = (int) (nWidth*0.95);
			if(controlPanel!=null)
			{ 
				if(layoutImageQL!=null && layoutImageQL.getVisibility()==View.VISIBLE){ 
					controlPanel.setVisibility(View.GONE); 
				}else{
					controlPanel.setVisibility(View.VISIBLE);
						 
				}
			}

			if(layoutCenter!=null)
			{
				 RelativeLayout.LayoutParams rlp=new RelativeLayout.LayoutParams(nWidth, nHeight);
				 rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
				 layoutCenter.setLayoutParams(rlp);
				 layoutCenter.setPadding(0, padding_in_px, 0, 0); 
			}
			
 			 nScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
			 for(int i = 0;i < Player.PLAYER_NUM;i++)
			 {
				 if(glViews[i]!=null)
				 {
    	    		glViews[i].onOreintationChange(nScreenOrientation);
				 }	 
    	    		
			 }
		}
 	}
}
	
    @Override
    public void onConfigurationChanged(Configuration config)
    {
     
    	super.onConfigurationChanged(config);//
    	
    	DisplayMetrics dm = getResources().getDisplayMetrics();   
		mScreenWidth = dm.widthPixels;//��Ļ��   
		mScreenHeight = dm.heightPixels;//��Ļ�� 
		LocalDefines.loadResource(getResources());//����ͼƬ��Դ
    	 
		if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
			/*
			 * If the screen is switched from portait mode to landscape mode
			 */
			ShowLandscapeView();
			
		}
		else if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
		{
			/*
			 * If the screen is switched from landscape mode to portrait mode
			 */
			ShowPortrailView();
		}
		System.out.println("onConfigurationChanged end");//add for test
     }
    
    
	  /**
     * ��ȡ�����е�����ID
     */
    public void InitGLContainers()
    {
    	//
    	top_bottom[Player.TOP] = findViewById(R.id.playContainerTop);
    	top_bottom[Player.BOTTOM] = findViewById(R.id.playContainerBottom);
    	//
    	parentContainers[Player.WND_ID_0] = findViewById(R.id.playContainerParent1);
    	parentContainers[Player.WND_ID_1] = findViewById(R.id.playContainerParent2);
    	parentContainers[Player.WND_ID_2] = findViewById(R.id.playContainerParent3);
    	parentContainers[Player.WND_ID_3] = findViewById(R.id.playContainerParent4);
    	//
    	backgroundContainers[Player.WND_ID_0] = findViewById(R.id.playContainer1background);
    	backgroundContainers[Player.WND_ID_1] = findViewById(R.id.playContainer2background);
    	backgroundContainers[Player.WND_ID_2] = findViewById(R.id.playContainer3background);
    	backgroundContainers[Player.WND_ID_3] = findViewById(R.id.playContainer4background);
    	//
    	containers[Player.WND_ID_0] = (LinearLayout)findViewById(R.id.playContainer1);
    	containers[Player.WND_ID_1] = (LinearLayout)findViewById(R.id.playContainer2);
    	containers[Player.WND_ID_2] = (LinearLayout)findViewById(R.id.playContainer3);
    	containers[Player.WND_ID_3] = (LinearLayout)findViewById(R.id.playContainer4);

    }
    
    void InitGLViewCloseButton()
    {
    	img_v[Player.WND_ID_0] = (ImageView) findViewById(R.id.close_but_0);
    	img_v[Player.WND_ID_1] = (ImageView) findViewById(R.id.close_but_1);
    	img_v[Player.WND_ID_2] = (ImageView) findViewById(R.id.close_but_2);
    	img_v[Player.WND_ID_3] = (ImageView) findViewById(R.id.close_but_3);

    }
    void SetCloseButtonVisible(boolean isVisible)
    {
    	if(isVisible)
    	{
	    	img_v[Player.WND_ID_0].setVisibility(View.VISIBLE);
	    	img_v[Player.WND_ID_1].setVisibility(View.VISIBLE);
	    	img_v[Player.WND_ID_2].setVisibility(View.VISIBLE);
	    	img_v[Player.WND_ID_3].setVisibility(View.VISIBLE);
    	}
    	else
    	{
	    	img_v[Player.WND_ID_0].setVisibility(View.GONE);
	    	img_v[Player.WND_ID_1].setVisibility(View.GONE);
	    	img_v[Player.WND_ID_2].setVisibility(View.GONE);
	    	img_v[Player.WND_ID_3].setVisibility(View.GONE);
    	}
    }
    
    /**
     * ���� GLES2SurfaceView��ʵ��
     */
    public void InitGLViewPlayer()
    {
    	int i = 0;
    	for(i = 0;i < Player.PLAYER_NUM;i++)
    	{
    		glViews[i] = new NVMediaPlayer(getApplication(), nScreenOrientation, i);
    		glViews[i].setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    	}
    }
    
    /**
     * ���� GLES2SurfaceView��ʵ��
     */
    public void ReleaseGLViewPlayer()
    {
    	int i = 0;
    	for(i = 0;i < Player.PLAYER_NUM;i++)
    	{
    		if(glViews[i]!=null)
    		{
    			glViews[i].DisableRender();
    			glViews[i] = null;
    		} 
    	}
    }
    
    //ֹͣ
    public void OnPlayersPuase()
    {
    	int i = 0;
    	for(i = 0;i < Player.PLAYER_NUM;i++)
    	{
    		if(glViews[i]!=null){
    			glViews[i].onPause();
    		}    		 
    	}
    }
    
    
    /**
     * ��OpenGL������ӵ�������
     */
    public void ConnectGLViewToPlayer()
    {
    	int i;
    	for (i = 0;i < Player.PLAYER_NUM;i++)
    	{
    		containers[i].addView(glViews[i]);
    	}
    	
    }
    
    /**
	 * ��ȡ������ID
	 */
	public void InitGLViewProgressbar()
	{
		Player.GetProgressBars((ProgressBar) findViewById(R.id.spinner_0), 
				Player.WND_ID_0);
		Player.GetProgressBars((ProgressBar) findViewById(R.id.spinner_1), 
				Player.WND_ID_1);
		Player.GetProgressBars((ProgressBar) findViewById(R.id.spinner_2), 
				Player.WND_ID_2);
		Player.GetProgressBars((ProgressBar) findViewById(R.id.spinner_3), 
				Player.WND_ID_3);

	}
	
	//��ʼ��
	private void InitGLViewTouchEventEX(){
		if(layoutCenter==null)return;
		layoutCenter.setLongClickable(true);
		layoutCenter.setOnTouchListener(this);
		
	}
	 /**
     *���ܰ�ť����¼� 
     */
    private void InitGLViewTouchEvent()
    {
     
    	//����_0˫��������
	    glViews[Player.WND_ID_0].setOnTouchListener(new OnTouchListener(){
	    	
	    	private long firstTouch = 0;
			private long secondTouch = 0;
			private boolean isFullScreen = false;
			//int count = 0;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				OnGLPlayViewSelected(Player.WND_ID_0);
				switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						secondTouch = event.getDownTime();						
						if(secondTouch - firstTouch <= Player.DOUBLE_CLICK_VAL)//�ж��Ƿ�Ϊ˫���¼�
						{
							 
							if(!isFullScreen)
							{
								isFullScreen = true;
								FullScreenMode(Player.WND_ID_0);
								LibContext.SwitchPlayingMode(Player.WND_ID_0, isFullScreen);
							}
							else
							{
								isFullScreen = false;
								ExitFullScreenMode(Player.WND_ID_0);
								LibContext.SwitchPlayingMode(Player.WND_ID_0, isFullScreen);
							}
						}
						else//�ж�Ϊ�����¼�
						{
							firstTouch = secondTouch;
							if(Player.isWindowSelected(Player.WND_ID_0) && Player.isWindowPlaying(Player.WND_ID_0))
							{
 								//GLES2PlayerLib.StopPlay(Player.WND_ID_0, false);
								//Player.setPlaying(Player.WND_ID_0, false);
								//Player.setRestart(Player.WND_ID_0);
							}
						}
						break;
					case MotionEvent.EDGE_LEFT: break;
					case MotionEvent.EDGE_RIGHT: break;
					default:break;
				}
				return false;
			}
	    });

	    //����_1˫��������
	    glViews[Player.WND_ID_1].setOnTouchListener(new OnTouchListener()
	    {
	    	private long firstTouch = 0;
			private long secondTouch = 0;
			private boolean isFullScreen = false;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				OnGLPlayViewSelected(Player.WND_ID_1);
				switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						secondTouch = event.getDownTime();
						if(secondTouch - firstTouch <= Player.DOUBLE_CLICK_VAL)//�ж��Ƿ�Ϊ˫���¼�
						{
 							if(!isFullScreen)
							{
								isFullScreen = true;
								FullScreenMode(Player.WND_ID_1);
								LibContext.SwitchPlayingMode(Player.WND_ID_1, isFullScreen);
							}
							else
							{
								isFullScreen = false;
								ExitFullScreenMode(Player.WND_ID_1);
								LibContext.SwitchPlayingMode(Player.WND_ID_1, isFullScreen);
							}
						}
						else
						{
							firstTouch = secondTouch;
							if(Player.isWindowSelected(Player.WND_ID_1) && Player.isWindowPlaying(Player.WND_ID_1))
							{
 //								GLES2PlayerLib.StopPlay(Player.WND_ID_1, false);
//								Player.setPlaying(Player.WND_ID_1, false);
							}

						}
						break;
					case MotionEvent.EDGE_LEFT: break;
					case MotionEvent.EDGE_RIGHT: break;
					default:break;
				}
				return false;
			}
	    });
	    
	    //����_2˫��������
	    glViews[Player.WND_ID_2].setOnTouchListener(new OnTouchListener()
	    {
	    	private long firstTouch = 0;
			private long secondTouch = 0;
			private boolean isFullScreen = false;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				OnGLPlayViewSelected(Player.WND_ID_2);

				switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						secondTouch = event.getDownTime();
						if(secondTouch - firstTouch <= Player.DOUBLE_CLICK_VAL)//�ж��Ƿ�Ϊ˫���¼�
						{
 							if(!isFullScreen)
							{
								isFullScreen = true;
								FullScreenMode(Player.WND_ID_2);
								LibContext.SwitchPlayingMode(Player.WND_ID_2, isFullScreen);
							}
							else
							{
								isFullScreen = false;
								ExitFullScreenMode(Player.WND_ID_2);
								LibContext.SwitchPlayingMode(Player.WND_ID_2, isFullScreen);
							}
						}
						else
						{
							firstTouch = secondTouch;
							if(Player.isWindowSelected(Player.WND_ID_2) && Player.isWindowPlaying(Player.WND_ID_2))
							{
 //								GLES2PlayerLib.StopPlay(Player.WND_ID_2, false);
//								Player.setPlaying(Player.WND_ID_2, false);
							}

						}
						break;
					case MotionEvent.EDGE_LEFT: break;
					case MotionEvent.EDGE_RIGHT: break;
					default:break;
				}
				return false;
			}
	    });
	    
	    //����_3˫��������
	    glViews[Player.WND_ID_3].setOnTouchListener(new OnTouchListener()
	    {
	    	private long firstTouch = 0;
			private long secondTouch = 0;
			private boolean isFullScreen = false;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				OnGLPlayViewSelected(Player.WND_ID_3);

				switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						secondTouch = event.getDownTime();
						if(secondTouch - firstTouch <= Player.DOUBLE_CLICK_VAL)//�ж��Ƿ�Ϊ˫���¼�
						{
 							if(!isFullScreen)
							{
								isFullScreen = true;
								FullScreenMode(Player.WND_ID_3);
								LibContext.SwitchPlayingMode(Player.WND_ID_3, isFullScreen);
							}
							else
							{
								isFullScreen = false;
								ExitFullScreenMode(Player.WND_ID_3);
								LibContext.SwitchPlayingMode(Player.WND_ID_3, isFullScreen);
							}
						}
						else
						{
							firstTouch = secondTouch;
							if(Player.isWindowSelected(Player.WND_ID_3) && Player.isWindowPlaying(Player.WND_ID_3))
							{
 //								GLES2PlayerLib.StopPlay(Player.WND_ID_3, false);
//								Player.setPlaying(Player.WND_ID_3, false);
							}

						}
						break;
					case MotionEvent.EDGE_LEFT: break;
					case MotionEvent.EDGE_RIGHT: break;
					default:break;
					
				}
				return false;
			}
	    });
	    
	    img_v[Player.WND_ID_0].setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("close","close player 0");
				glViews[Player.WND_ID_0].StopPlay();//add by luo 20141008
				Player.ClearGLESScreen(glViews, false, Player.WND_ID_0);
				return false;
			}
	    	
	    });
	    
	    img_v[Player.WND_ID_1].setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("close","close player 1");
				 glViews[Player.WND_ID_1].StopPlay();//add by luo 20141008
					Player.ClearGLESScreen(glViews, false, Player.WND_ID_1);
				return false;
			}
	    	
	    });
	    
	    img_v[Player.WND_ID_2].setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("close","close player 2");
				 glViews[Player.WND_ID_2].StopPlay();//add by luo 20141008
					Player.ClearGLESScreen(glViews, false, Player.WND_ID_2);
				return false;
			}
	    	
	    });

	    img_v[Player.WND_ID_3].setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("close","close player 3");
				 glViews[Player.WND_ID_3].StopPlay();//add by luo 20141008
					Player.ClearGLESScreen(glViews, false, Player.WND_ID_3);
				return false;
			}
	    	
	    });

    }
    
    /**
     * ����ѡ�д��ڣ���ѡ�д�����Χ��ӱ߿򣬲���ȡ�������κ�һ���Ѿ�
     * ѡ��Ĵ��ڡ�
     * @param WndID	��ѡ�д��ں�
     */
    public void OnGLPlayViewSelected(int WndID)
    {
    	switch(WndID)
    	{
    		case Player.WND_ID_0:

    			Player.SelectWindow(Player.WND_ID_0);
    			//�������ڸ����߿�
    			backgroundContainers[Player.WND_ID_0].setVisibility(View.VISIBLE);
    			//ȡ�������κ�һ�����ڵĸ����߿�
    			if(backgroundContainers[Player.WND_ID_1].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_2].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_3].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			}    			
    			break;
    		case Player.WND_ID_1:
    			
    			Player.SelectWindow(Player.WND_ID_1);
    			//�������ڸ����߿�
    			backgroundContainers[Player.WND_ID_1].setVisibility(View.VISIBLE);
    			//ȡ�������κ�һ�����ڵĸ����߿�
    			if(backgroundContainers[Player.WND_ID_0].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_0].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_2].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_3].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			}    			

    			break;
    		case Player.WND_ID_2:
    			
    			Player.SelectWindow(Player.WND_ID_2);
    			//�������ڸ����߿�
    			backgroundContainers[Player.WND_ID_2].setVisibility(View.VISIBLE);
    			//ȡ�������κ�һ�����ڵĸ����߿�
    			if(backgroundContainers[Player.WND_ID_0].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_0].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_1].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_3].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			}    			

    			break;
    		case Player.WND_ID_3:
    			
    			Player.SelectWindow(Player.WND_ID_3);
    			//�������ڸ����߿�
    			backgroundContainers[Player.WND_ID_3].setVisibility(View.VISIBLE);
    			//ȡ�������κ�һ�����ڵĸ����߿�
    			if(backgroundContainers[Player.WND_ID_0].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_0].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_1].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			}
    			if(backgroundContainers[Player.WND_ID_2].getVisibility() == View.VISIBLE)
    			{
    				backgroundContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			}    			

    			break;
			default:break;
    	}
    }
    
    
    
    /**
     * ����ȫ��ģʽ
     * @param WndID	ȫ���Ĵ��ں�
     */
    public void FullScreenMode(int WndID)
    {
    	switch(WndID)
    	{
    		/*
    		 * ����_0����ȫ��ģʽ�����δ���_1,����_2,����_3��
    		 * ���صײ��������ͱ���_1,����_2������_3
    		 */
    		case Player.WND_ID_0:
    			//���ش���_1
    			top_bottom[Player.TOP].setVisibility(View.VISIBLE);
    			parentContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			containers[Player.WND_ID_1].setVisibility(View.GONE);
    			glViews[Player.WND_ID_1].setVisibility(View.GONE);
    			
    			//���ش���_2
    			top_bottom[Player.BOTTOM].setVisibility(View.GONE);
    			parentContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			containers[Player.WND_ID_2].setVisibility(View.GONE);
    			glViews[Player.WND_ID_2].setVisibility(View.GONE);
    			
    			//���ش���_3
    			parentContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			containers[Player.WND_ID_3].setVisibility(View.GONE);
    			glViews[Player.WND_ID_3].setVisibility(View.GONE);
    			break;
        		/*
        		 * ����_1����ȫ��ģʽ�����δ���_0,����_2,����_3��
        		 * ���صײ��������ͱ���_0,����_2������_3
        		 */
    		case Player.WND_ID_1:
    			//���ش���_0
    			top_bottom[Player.TOP].setVisibility(View.VISIBLE);
    			parentContainers[Player.WND_ID_0].setVisibility(View.GONE);
    			containers[Player.WND_ID_0].setVisibility(View.GONE);
    			glViews[Player.WND_ID_0].setVisibility(View.GONE);
    			//���ش���_2
    			top_bottom[Player.BOTTOM].setVisibility(View.GONE);
    			parentContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			containers[Player.WND_ID_2].setVisibility(View.GONE);
    			glViews[Player.WND_ID_2].setVisibility(View.GONE);
    			//���ش���_3
    			parentContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			containers[Player.WND_ID_3].setVisibility(View.GONE);
    			glViews[Player.WND_ID_3].setVisibility(View.GONE);
    			break;
        		/*
        		 * ����_2����ȫ��ģʽ�����δ���_0,����_1,����_3��
        		 * ���صײ��������ͱ���_0,����_1������_3
        		 */
    		case Player.WND_ID_2:
    			//���ش���_0
    			top_bottom[Player.TOP].setVisibility(View.GONE);
    			parentContainers[Player.WND_ID_0].setVisibility(View.GONE);
     			containers[Player.WND_ID_0].setVisibility(View.GONE);
    			glViews[Player.WND_ID_0].setVisibility(View.GONE);
    			//���ش���_1
    			parentContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			containers[Player.WND_ID_1].setVisibility(View.GONE);
    			glViews[Player.WND_ID_1].setVisibility(View.GONE);
    			//���ش���_3
    			top_bottom[Player.BOTTOM].setVisibility(View.VISIBLE);
    			parentContainers[Player.WND_ID_3].setVisibility(View.GONE);
    			containers[Player.WND_ID_3].setVisibility(View.GONE);
    			glViews[Player.WND_ID_3].setVisibility(View.GONE);
    			break;
        		/*
        		 * ����_3����ȫ��ģʽ�����δ���_0,����_1,����_2��
        		 * ���صײ��������ͱ���_0,����_1������_2
        		 */
    		case Player.WND_ID_3:
    			//���ش���_0
    			top_bottom[Player.TOP].setVisibility(View.GONE);
    			parentContainers[Player.WND_ID_0].setVisibility(View.GONE);
    			containers[Player.WND_ID_0].setVisibility(View.GONE);
    			glViews[Player.WND_ID_0].setVisibility(View.GONE);
    			//���ش���_1
    			parentContainers[Player.WND_ID_1].setVisibility(View.GONE);
    			containers[Player.WND_ID_1].setVisibility(View.GONE);
    			glViews[Player.WND_ID_1].setVisibility(View.GONE);
    			//���ش���_2
    			top_bottom[Player.BOTTOM].setVisibility(View.VISIBLE);
    			parentContainers[Player.WND_ID_2].setVisibility(View.GONE);
    			containers[Player.WND_ID_2].setVisibility(View.GONE);
    			glViews[Player.WND_ID_2].setVisibility(View.GONE);
    			break;
			default:break;
    	}
    }
    
    /**
     * �˳�ȫ��ģʽ
     * @param WndID	�˳�ȫ���Ĵ��ں�
     */
    public void ExitFullScreenMode(int WndID)
    {
    	/*
    	 * �˳�ȫ��ģʽ���ָ������͵ײ������������������ָ�
    	 * ��������
    	 */
    	//�ָ���ʾ����_0
    	top_bottom[Player.TOP].setVisibility(View.VISIBLE);
    	parentContainers[Player.WND_ID_0].setVisibility(View.VISIBLE);
		containers[Player.WND_ID_0].setVisibility(View.VISIBLE);
		glViews[Player.WND_ID_0].setVisibility(View.VISIBLE);
		//�ָ���ʾ����_1
    	parentContainers[Player.WND_ID_1].setVisibility(View.VISIBLE);
		containers[Player.WND_ID_1].setVisibility(View.VISIBLE);
		glViews[Player.WND_ID_1].setVisibility(View.VISIBLE);
		//�ָ���ʾ����_2
    	top_bottom[Player.BOTTOM].setVisibility(View.VISIBLE);
    	parentContainers[Player.WND_ID_2].setVisibility(View.VISIBLE);
		containers[Player.WND_ID_2].setVisibility(View.VISIBLE);
		glViews[Player.WND_ID_2].setVisibility(View.VISIBLE);
		//�ָ���ʾ����_3
    	parentContainers[Player.WND_ID_3].setVisibility(View.VISIBLE);
		containers[Player.WND_ID_3].setVisibility(View.VISIBLE);
		glViews[Player.WND_ID_3].setVisibility(View.VISIBLE);
    }
    
    
	public void SetGLViewPlayerMessageHandler()
	{
		int i;
		for (i = 0;i < Player.PLAYER_NUM;i++)
		{
			glViews[i].GetHandler(handler);
		}
		 
	}
	
	//�̸߳ı�uiʱ����
	private Handler handler = new Handler()
	{
		//@SuppressLint("HandlerLeak")
		public void handleMessage(Message msg) 
		{	
			// add by mai 2015-6-25 ��ͼ��ɿ����ɵ��
			if(msg.arg1 == BTN_SCREENSHOT)
			{
				mBtnScreenShot.setEnabled(true);
				mBtnScreenShot2.setEnabled(true);
				return;
			}
			//end add by mai 2015-6-25
			// add by mai 2015-7-31 Ԥ��λ���óɹ�
			if(msg.arg1 == PTZX_RESULT_OK)
			{
				progressBarPresetConfig.setVisibility(View.GONE);
				int nPTZXID = msg.arg2;
				boolean isSetOK = false;
 				
				if(nPTZXID>=0 && nPTZXID<9){
					Bitmap image = getPTZXImage();
					isSetOK = LocalDefines.updatePTZXPoints(m_nDeviceID, nPTZXID, image);
				}
				 
				Toast.makeText(NVPlayerPlayActivity.this, getString(R.string.presetOK), Toast.LENGTH_SHORT).show();
				
				if(ptzxAdapter!=null){
					 ptzxAdapter.notifyDataSetChanged();
 				 }
				
				return;
				
			}else if(msg.arg1 == PTZX_RESULT_FAIL){
				progressBarPresetConfig.setVisibility(View.GONE);
				Toast.makeText(NVPlayerPlayActivity.this,  getString(R.string.presetFail), Toast.LENGTH_SHORT).show();
				return;
			}
			
			//end add by mai 2015-7-21
			
			//add by luo 20141226
			if(msg.arg1 == Defines.MSG_SCREENSHOT){
				 ScreenShot();
					 
				return;
			}
			//end add by luo20141226
			
			if(msg.arg1 == Defines.MSG_HIDE_TOOLVIEW){
				if(!mIsSpeaking && layoutImageQL.getVisibility()!=View.VISIBLE && mPTZPanel.getVisibility()!=View.VISIBLE && nScreenOrientation== Configuration.ORIENTATION_LANDSCAPE){
					if(popupWindowMore!=null && popupWindowMore.isShowing()){//����ҳ������������ض���
						nToolsViewShowTickCount=5;
						return;
					}
					hideToolsViews();  
				}
					 
				return;
			}
			//@@System.out.println("handleMessage");//add for test
			switch(msg.arg2)  //���Ų��
			{
				case Player.WND_ID_0:
					if(msg.arg1 == 1)
					{
						Player.ShowProgressBar(Player.WND_ID_0, true);
					}
					else
					{
						Player.ShowProgressBar(Player.WND_ID_0, false);
					}
					break;
				case Player.WND_ID_1:
					if(msg.arg1 == 1)
					{
						Player.ShowProgressBar(Player.WND_ID_1, true);
					}
					else
					{
						Player.ShowProgressBar(Player.WND_ID_1, false);
					}
					break;
				case Player.WND_ID_2:
					if(msg.arg1 == 1)
					{
						Player.ShowProgressBar(Player.WND_ID_2, true);
					}
					else
					{
						Player.ShowProgressBar(Player.WND_ID_2, false);
					}
					break;
				case Player.WND_ID_3:
					if(msg.arg1 == 1)
					{
						Player.ShowProgressBar(Player.WND_ID_3, true);
					}
					else
					{
						Player.ShowProgressBar(Player.WND_ID_3, false);
					}
					break;
				default:break;
			}
		}
	};
	
	static final int SIZE_CMDPACKET=128;
	static final int SEND_BUFFER_SIZE=512;
	static final int SESSION_FRAME_BUFFER_SIZE=65536;
	static final int SEND_BUFFER_DATA_SIZE=504; // 512 - 8 (header)
	static final int SEND_BUFFER_HEADER_SIZE=8;
	static final int SP_DATA = 0x7f;
	
	static final int  CMD_REQUEST=0x9101;
	static final int  CMD_AFFIRM=0x9102;
	static final int  CMD_EXIT=0x9103;
	static final int  CMD_ACCEPT=0x9104;
	static final int  CMD_CONNECTINFO=0x9105;
	static final int  CMD_STREAMHEAD=0x9106;
	static final int  CMD_UDPSHAKE=0x9107;
	static final int  CMD_ASKFORCNLNUM=0x9108;
	static final int  CMD_CHECKPSW=0x9109;
	//��ȡͼ��
	//��ȡͼ��
	 
	Bitmap bm = null; 
	String folderName="iCamSeeImages";
	
	// add by mai 2015-6-25 �����ӳٽ�ͼ
	private int m_nThreadID = 0;
	private class ThreadBtnScreenShot extends Thread{

		private int nThreadID = 0;
		public ThreadBtnScreenShot(int nThreadID) {
			
			this.nThreadID = nThreadID;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(m_nThreadID == nThreadID) //�����ǰactivityû���˳���ˢ��UI
			{
				Message msg=handler.obtainMessage();
				msg.arg1=BTN_SCREENSHOT;
				handler.sendMessage(msg);
			}
			
			
		}
		
		
		
	}
	// end by mai 2015-6-25 
	//����ͼƬ��sd��
	private boolean saveToSDCard(Bitmap image, String strFileName){
		boolean bResult = false;
		
		if(image==null) return bResult;
			
		try {
		
			File file = new File("/sdcard/hongshi");
			if (!file.exists()) file.mkdir();
			 
	     	File imageFile = new File("/sdcard/hongshi/"+strFileName);    	
	     	FileOutputStream out = new FileOutputStream(imageFile);
	     	image.compress(Bitmap.CompressFormat.JPEG, 90, out);
	     	out.flush();
	     	out.close();
	     	bResult = true;

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		 
		return bResult;
	}
	//end add by luo 20141226
	
	//ͼƬ
	private Bitmap getPTZXImage(){
		 
		//���ȱ��浽���
		bm = glViews[Player.CurrentSelPlayer()].Screenshot();//��ͼ
 
		float deltaW = Defines._PTZXWidth/Defines._capWidth;
		float deltaH = Defines._PTZXHeight/Defines._capHeight;
		Matrix matrix = new Matrix(); 
		matrix.postScale(deltaW,deltaH); //���Ϳ�Ŵ���С�ı���
		Bitmap resizeBmp = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),matrix,true);  
		return resizeBmp;
	}
	
	//add by luo 20141115 ��Ļ��ͼ
	private void ScreenShot(){
		
			m_nThreadID++; //�����ӳ��´ε����ͼ����
			mBtnScreenShot.setEnabled(false);  // add by mai 2015-6-25 ͼƬû�б�����֮ǰ���ɵ��
			mBtnScreenShot2.setEnabled(false);  // add by mai 2015-6-25 ͼƬû�б�����֮ǰ���ɵ��
			new ThreadBtnScreenShot(m_nThreadID).start(); // add by mai 2015-6-25 �����߳��ӳٸı�״̬
			//û��SDCard
			String strSavePath = Functions.GetSDPath();
			if(strSavePath==null)
			{
				screenshotDialog.dismiss();
				Toast.makeText(getApplicationContext(), getString(R.string.noticeSDCardNotExist), 
					Toast.LENGTH_SHORT).show();
				return;
			}
			//���ȱ��浽���
			bm = glViews[Player.CurrentSelPlayer()].Screenshot();//��ͼ
			
			if(bm!=null)
			{
				
				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
				String dateString = formatter.format(new Date());
		 
				String strFileName = dateString+"("+m_strName+")"+".jpg";

				if(saveToSDCard(bm, strFileName)){	 //���浽ָ���ļ���  add by mai 2015-4-9
					
					screenshotDialog.dismiss();
					Toast.makeText(getApplicationContext(), getString(R.string.noticeFileSaveToAlbumsOK), 
							   Toast.LENGTH_SHORT).show();
				}else{
					
					strFileName = strSavePath+"/"+strFileName;
					if(saveToSDCard(bm, strFileName)){
						
						screenshotDialog.dismiss();
						Toast.makeText(getApplicationContext(), getString(R.string.noticeScreenShotOK), 
								   Toast.LENGTH_SHORT).show();
						
					}else{
						
						screenshotDialog.dismiss();
						Toast.makeText(getApplicationContext(),  getString(R.string.noticeScreenShotFail), 
								   Toast.LENGTH_SHORT).show();
					}
				}
			}else{//����ʧ��
				screenshotDialog.dismiss();
				
				Toast.makeText(getApplicationContext(),  getString(R.string.noticeScreenShotFail), 
						   Toast.LENGTH_SHORT).show();
			}
			
			
		 
		}
		//end add by luo 20141115
		
	 
	
	 @SuppressWarnings("deprecation")
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //������Ļ����
        NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);		
        notiManager.cancel(LocalDefines.notificationID);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_nvplayer_playview);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
         // �����µ�ͼƬ  
        DisplayMetrics dm = getResources().getDisplayMetrics();   
			
		mScreenWidth = dm.widthPixels;//��Ļ��   
		mScreenHeight = dm.heightPixels;//��Ļ�� 
		LocalDefines.loadResource(getResources());//����ͼƬ��Դ
		
		FLING_MAX_DISTANCE = (int) (mScreenWidth/3);
		
     	tvPlayDeviceID = (TextView) findViewById(R.id.tvPlayDeviceID);  //add by mai 2015-3-13
     	llPlayTalkback = (LinearLayout) findViewById(R.id.llPlayTalkback); // add by mai 2015-3-14
        Bundle data = this.getIntent().getExtras();
        if(data!=null)
        {
        	//�õ��ϼ��������Ĳ���
        	 m_strName = data.getString("name");
        	 deviceParam = data.getParcelable("device_param");
        	 m_bReversePRI= deviceParam.isbReversePRI();//add  by luo 20141219
        	 m_bPTZ  = deviceParam.isbPTZ();
          	 m_bPTZX = deviceParam.isbPTZX();
          	 m_nPTZXCount = deviceParam.getnPTZXCount();
          	 m_nDeviceID = deviceParam.getnDeviceID();
          	 m_strUsername=data.getString("username");
          	 m_strPassword=data.getString("password");
          
          	 m_bSpeak = deviceParam.isbSpeak();
        	//end add by luo 20141008
        }
        
        LocalDefines.loadPTZXPoints(deviceParam.getnDeviceID());
        LayoutMicroPhoneBottom=(LinearLayout)findViewById(R.id.MicroPhoneLayoutBottom);   
        mGestureDetector = new GestureDetector(this, new PTZGestureListener(this));
        
        mScaleGestureDetector =new ScaleGestureDetector(this, new ScaleGestureListener());  
        tvPlayDeviceID.setText(m_strName);
        
        layoutTopBar = (LinearLayout)findViewById(R.id.linearLayoutTopBar);
        layoutCenter =  (LinearLayout)findViewById(R.id.playContainer);
        layoutBottomBar = (LinearLayout)findViewById(R.id.linearLayoutBottomBar);
         
        btnPTZLeft = (Button) findViewById(R.id.btnPTZLeft);
        btnPTZLeft.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_left_1))); //add by mai 2015-7-9 ��̬��ӱ���
        btnPTZLeft.setOnTouchListener(this);
        btnPTZRight = (Button) findViewById(R.id.btnPTZRight);
        btnPTZRight.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_right_1))); //add by mai 2015-7-9 ��̬��ӱ���
        btnPTZRight.setOnTouchListener(this);
        btnPTZUP = (Button) findViewById(R.id.btnPTZUP);
        btnPTZUP.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_top_1))); //add by mai 2015-7-9 ��̬��ӱ���
        btnPTZUP.setOnTouchListener(this);
        btnPTZDown = (Button) findViewById(R.id.btnPTZDown);
        btnPTZDown.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_bottom_1))); //add by mai 2015-7-9 ��̬��ӱ���
        btnPTZDown.setOnTouchListener(this);
        llLandscape = (LinearLayout) findViewById(R.id.llLandscape);
        llVertical = (LinearLayout) findViewById(R.id.llVertical);
        llVertical.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_back_button_bg_alpha)));
        llLandscapeDefinition = (LinearLayout) findViewById(R.id.llLandscapeDefinition);
        llLandscapeDefinition.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_back_button_bg_alpha)));
        rgLandscapeDefinition = (RadioGroup) findViewById(R.id.rgLandscapeDefinition);

        // add by mai 2015-7-30
        btnPresetConfig = (ImageView) findViewById(R.id.btnPresetConfig);
        btnPresetConfig.setOnClickListener(this);
        
        ivPresetLandscape = (ImageView) findViewById(R.id.ivPresetLandscape);
        ivPresetLandscape.setOnClickListener(this);
        // end add by mai 2015-7-30

         /////////////////////////////////////////////////
        InitGLContainers();
        InitGLViewProgressbar();
         // ����OpenGl���棬��������ȾģʽΪ������Ⱦ
        InitGLViewPlayer();
         // ��� OpenGL ES2.0���浽����
        ConnectGLViewToPlayer();
         //CreateHandler();
        SetGLViewPlayerMessageHandler();
 
        Player.ClearGLESScreen(glViews,true,0);
        InitGLViewCloseButton();
        InitGLViewTouchEventEX();   //��ť����¼�����ʱ�����
        //	���û�������
        LibContext.SetContext(glViews[Player.WND_ID_0],glViews[Player.WND_ID_1],glViews[Player.WND_ID_2],glViews[Player.WND_ID_3]);
	     
	    ShowPortrailView();
	    Player.SelectWindow(Player.WND_ID_0);
	    FullScreenMode(Player.WND_ID_0);
 
	    System.out.println("onCreate 3");

    	mBtnBack = (ImageView)findViewById(R.id.buttonBackToLogin);
    	mBtnBack.setOnClickListener(this);
    	
    	mBtnStopAndPlay = (Button)findViewById(R.id.buttonStopAndPlay);
    	mBtnStopAndPlay.setOnClickListener(this);
    	
    	mBtnSound =  (Button)findViewById(R.id.buttonSound);
    	mBtnSound.setOnClickListener(this);
    	
    	mBtnMic =  (Button)findViewById(R.id.buttonMic);
    	mBtnMic.setOnTouchListener(this);
    	
    	mBtnReverse= (Button)findViewById(R.id.buttonReverse);
    	mBtnReverse.setOnClickListener(this);
    	
    	mBtnScreenShot= (Button)findViewById(R.id.buttonScreenShot);
    	mBtnScreenShot.setOnClickListener(this);
    	
    	
    	//add by mai 2015-3-25
    	mBtnScreenShot2= (Button)findViewById(R.id.buttonScreenShot2);
    	mBtnScreenShot2.setOnClickListener(this);
    	
    	mBtnMic2 =  (Button)findViewById(R.id.buttonMic2);
    	mBtnMic2.setOnTouchListener(this);
    	
    	mBtnReverse2= (Button)findViewById(R.id.buttonReverse2);
    	mBtnReverse2.setOnClickListener(this);
    	
    	mBtnSound2 =  (Button)findViewById(R.id.buttonSound2);
    	mBtnSound2.setOnClickListener(this);
    	
    	mBtnBack2 = (Button)findViewById(R.id.buttonBackToLogin2);
    	mBtnBack2.setOnClickListener(this);
    	// end add by mai 2015-3-25
    	
    	
    	//add by luo 20141014
    	if(m_bSpeak){ //�Ƿ��жԽ�����
    		findViewById(R.id.layoutMicBtn).setVisibility(View.VISIBLE);
    		findViewById(R.id.layoutMicBtn2).setVisibility(View.VISIBLE);  //add by mai 2015-3-25
    	}else{
    		findViewById(R.id.layoutMicBtn).setVisibility(View.GONE);
    		findViewById(R.id.layoutMicBtn2).setVisibility(View.GONE);  // add by mai 2015-3-25
    	}
    	
    	controlPanel = (RelativeLayout)findViewById(R.id.ptzCtrlPanel); //add by mai 2015-3-14
    	findViewById(R.id.linearLayoutPTZNotice).setVisibility(View.GONE);
    
    	mPTZPanel = (LinearLayout)findViewById(R.id.toolPTZ); 
    	mPTZPanel.setVisibility(View.GONE);
     	mBtnImageQl =  (Button)findViewById(R.id.buttonImageGQL);
    	mBtnImageQl.setOnClickListener(this);
    	
    	mBtnImageQl2 =  (Button)findViewById(R.id.buttonImageGQL2); // add by mai 2015-3-25
    	mBtnImageQl2.setOnClickListener(this);  // add by mai 2015-3-25
     	
    	layoutImageQL = (LinearLayout)findViewById(R.id.layoutImageQL); 
		setQLViewVisible(false);
		
    	mBtnHD = (RadioButton)findViewById(R.id.rBtnHD);
    	btnHD = (Button)findViewById(R.id.btnHD);  // add by mai 2015-3-25
    	mBtnSmooth = (RadioButton)findViewById(R.id.rBtnSmooth);
    	btnSmooth = (Button)findViewById(R.id.btnSmooth);  // add by mai 2015-3-25
    	mBtnHD.setOnClickListener(this);
    	mBtnSmooth.setOnClickListener(this);
    	btnHD.setOnClickListener(this);  // add by mai 2015-3-25
    	btnSmooth.setOnClickListener(this);  // add by mai 2015-3-25
    	
    	if(mQLHD){
    		mBtnImageQl.setVisibility(View.VISIBLE);
    		mBtnImageQl2.setVisibility(View.VISIBLE); // add by mai 2015-3-25
    	}else{
    		mBtnImageQl.setVisibility(View.GONE);
    		mBtnImageQl2.setVisibility(View.GONE);  // add by mai 2015-3-25
    	}
    	
    	if(mStreamType==0){//��ǰ�Ǹ��廹����
    		mBtnSmooth.setChecked(true);
    		btnSmooth.setTextColor(Color.BLACK);
    		btnHD.setTextColor(Color.BLUE);
    	}else{
    		mBtnHD.setChecked(true);
    		btnSmooth.setTextColor(Color.BLUE);
    		btnHD.setTextColor(Color.BLACK);
    	}
    	readSystemParam(); //��ȡ�ϴ���ŵ�
    	onSoundChange(); //������ر�����	
     	
    	mBtnVerticalAuto = (RadioButton)findViewById(R.id.rBtnVerticalAuto); 
    	mBtnHorizontalAuto = (RadioButton)findViewById(R.id.rBtnHorizontalAuto);
    	mBtnVerticalAuto.setOnClickListener(this);
    	mBtnHorizontalAuto.setOnClickListener(this);
    	
     	//�ָ�����ͨ��
    	mPlayingChn=1;
     	mIsPlaying = true;
     	createDialogs(); 
     }
 
    
    /**
     * add by mai 2015-7-30
     * Ԥ��λʹ��������
     */
    @SuppressWarnings("deprecation")
	private void presetPopuwindow(boolean showTooView)
    {
    	LayoutInflater layoutInflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.ptzx_pannel_popupwindow, null);
		
		lvPreset = (ListView) view.findViewById(R.id.lvPreset);
		Button btnPresetCancel = (Button) view.findViewById(R.id.btnPresetCancel);
		progressBarPresetConfig = (ProgressBar) view.findViewById(R.id.progressBarPresetConfig);
		LinearLayout llTopTitle = (LinearLayout) view.findViewById(R.id.llTopTitle);
		
		presetList();
		
		//add by luo 20150803
		if(showTooView){
			btnPresetCancel.setVisibility(View.VISIBLE);
			llTopTitle.setVisibility(View.VISIBLE);
		}else{
			btnPresetCancel.setVisibility(View.GONE);
			llTopTitle.setVisibility(View.GONE);
		}
		//end add by luo 20150803
		/**
		 * ���ؼ�����¼�
		 */
		btnPresetCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(popupWindowMore != null)
				{
					popupWindowMore.dismiss();
				}
			}
		});

		int window = mScreenHeight*2/3;
		int onePreset_dp = 60;  //
		int title_dp = 40;
		
		final float scale = getResources().getDisplayMetrics().density;
		int title_px = (int) (title_dp * scale + 0.5f);
		int onePreset_px = (int) (onePreset_dp * scale + 0.5f);
		int presetHeight = onePreset_px*6+title_px; //���ݸ������ж�̬������С
		if(presetHeight > window)
		{ //�����ǰ������ռ��С������Ļ������֮����Ϊ����֮��
			presetHeight = window;
		}
		
		if(mScreenHeight < mScreenWidth)
		{
			popupWindowMore = new PopupWindow(view, mScreenHeight*4/5, LayoutParams.MATCH_PARENT);
		}else{
			popupWindowMore = new PopupWindow(view,LayoutParams.MATCH_PARENT, presetHeight);
		}
		
		
		popupWindowMore.setFocusable(true); // ʹ��۽�
		popupWindowMore.setOutsideTouchable(true); // ����������������ʧ
		popupWindowMore.setBackgroundDrawable(new BitmapDrawable()); // ������ؼ�BackҲ�ܹ�ʹ����ʧ������Ӱ�챳��
		popupWindowMore.showAtLocation(view, Gravity.BOTTOM|Gravity.LEFT, 0, 0);
    	
    }
    
 // ���¼̳�getview����дListView Adapter,�Դ���listview�а�ť�ĵ���¼�
 	private class PTZXPiontAdapter extends BaseAdapter {

 		private class ItemViewHolder {
 			ImageView ivPresetImage;
 			RoundCornerImageView btnPTZXSet;
 			TextView tvPresetText;
 			TextView tvIDText;
 		}

 		private ArrayList<Bitmap> mAppList;
 		private LayoutInflater mInflater;
 		private Context mContext;
 		private String[] keyString;
 		private int[] valueViewID;
 		private ItemViewHolder holder;

 		public PTZXPiontAdapter(Context c,
 				ArrayList<Bitmap> appList, int resource,
 				String[] from, int[] to) {
 			mAppList = appList;
 			mContext = c;
 			mInflater = (LayoutInflater) mContext
 					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 			keyString = new String[from.length];
 			valueViewID = new int[to.length];
 			System.arraycopy(from, 0, keyString, 0, from.length);
 			System.arraycopy(to, 0, valueViewID, 0, to.length);
 			
 		}

 		@Override
 		public int getCount() {
 			return mAppList.size();
 		}

 		@Override
 		public Object getItem(int position) {
 			return mAppList.get(position);
 		}

 		@Override
 		public long getItemId(int position) {
 			return position;
 		}

 		@Override
 		public View getView(int position, View convertView, ViewGroup parent) {

 			if (convertView != null) {
 				holder = (ItemViewHolder) convertView.getTag();
 			} else {
 				convertView = mInflater
 						.inflate(R.layout.preset_count_item, null);
 				holder = new ItemViewHolder();
 				holder.ivPresetImage = (ImageView) convertView
 						.findViewById(valueViewID[0]);
 				holder.btnPTZXSet = (RoundCornerImageView) convertView
 						.findViewById(valueViewID[1]);
 				holder.tvPresetText = (TextView) convertView
 						.findViewById(valueViewID[2]);
 				holder.tvIDText = (TextView) convertView
 						.findViewById(valueViewID[3]);
 				
 				convertView.setTag(holder);
 			}

 			 
 		   PTZXPoint ptzxPoint =  null;
 		   Bitmap image = null;
 		   if(LocalDefines._PTZXPointDevID == m_nDeviceID){
 			   
  			   
 			  ptzxPoint = LocalDefines._ptzxPointList.get(position);
 		   }
 			
 		   if(ptzxPoint!=null){
 			   image = ptzxPoint.getFaceImage();
 			 
 			   
 		   }
 		   
 		  if(image!=null){
 			 holder.ivPresetImage.setImageBitmap(image);
 			 holder.tvPresetText.setVisibility(View.GONE);
 		  }else{
 			  holder.tvPresetText.setVisibility(View.VISIBLE);
 			 holder.ivPresetImage.setImageBitmap(null);
 		   }
 			ListViewButtonListener listener = new ListViewButtonListener(position);
 
 			holder.tvIDText.setText(""+(position+1));
 			holder.ivPresetImage.setOnClickListener(listener);
 			holder.btnPTZXSet.setOnClickListener(listener);
 			
 				
 			return convertView;
 		}

 		class ListViewButtonListener implements OnClickListener {
 			private int position;

 			ListViewButtonListener(int pos) {
 				position = pos;
 			}

 			@Override
 			public void onClick(View v) {
 				
 			
 				
 				if(v.getId() == holder.ivPresetImage.getId())
 				{ //���Ԥ��λͼƬ
 				 	
 					locationPTZXPoint(position);
 					if(popupWindowMore !=null)
 					{
 						popupWindowMore.dismiss();
 					}
 				}else if(v.getId() == holder.btnPTZXSet.getId())
 				{ //�������Ԥ��λ��ť
 					progressBarPresetConfig.setVisibility(View.VISIBLE);
 					
 					startPTZXConfig(deviceParam, position, Defines.NV_PRESET_ACTION_RESET);
 					
 				}
 				
 				
 		
 			}
 		}
 	}
    
  /**
   * ��װ����
   */
  private void presetList()
  {
	  
	  if(m_nPTZXCount > 0)
	  {
		  ArrayList<Bitmap> listItem = new ArrayList<Bitmap>();
		  for(int i=0; i<m_nPTZXCount; i++)
		  {
			 listItem.add(Functions.readBitMap(this, R.drawable.alarm_icon));
			 
		  }

		  ptzxAdapter = new PTZXPiontAdapter(NVPlayerPlayActivity.this,
				listItem,R.layout.preset_count_item,// ListItem��XMLʵ��
				// ��̬������ImageItem��Ӧ������
				new String[] { "ItemPresetImage", "ItemPresetSetImage","ItemPresetText","ItemIDText"},
				// ImageItem��XML�ļ������һ��ImageView,����TextView ID
				new int[] { R.id.ivPresetImage, R.id.btnPTZXSet, R.id.tvPresetText, R.id.tvPTZXID});

		  if (lvPreset != null) {
				lvPreset.setCacheColorHint(Color.TRANSPARENT);
				// ��Ӳ�����ʾ
				lvPreset.setAdapter(ptzxAdapter);
				// ��ӵ��
				lvPreset.setOnItemClickListener(this);
		  }

	  } else {

	 }
		  
  }
 	
    //ֹͣ����
    private void stopPlay(boolean bFlag){
    	ptzTimerThreadID++;
    	//add by luo 20141007
    	if(m_nAddType!=Defines.SERVER_SAVE_TYPE_DEMO)
    	{
			bm = glViews[Player.CurrentSelPlayer()].Screenshot();//��ͼ
	    		if(bm!=null)
	    		{
 	    			Bitmap image = Functions.zoomBitmap(bm, 160, 120);
 	    			if(m_nDeviceID>0){
	        			DatabaseManager.setFaceForDevID(m_nDeviceID, image,  m_strUsername, m_strPassword);
	        		}else{
	        			DatabaseManager.setFaceForID(m_nID, image);
	        		}
 	    		}
 
    	}
    	 
    	//end add by luo 20141007
    	 
    	mIsPlaying = false;
    	tvPlayDeviceID.setText(m_strName);
		mPlayingChn = -1;
		   
		glViews[Player.CurrentSelPlayer()].scale(1, 1);
		glViews[Player.CurrentSelPlayer()].StopPlay();//add by luo 20141008

	    Player.ShowProgressBar(Player.WND_ID_0, false);
	    Player.ShowProgressBar(Player.WND_ID_1, false);
	    Player.ShowProgressBar(Player.WND_ID_2, false);
	    Player.ShowProgressBar(Player.WND_ID_3, false);
		Player.ClearGLESScreen(glViews,true,0);
    }
    //��ʼ����
	private void startPlay(){
    	if(m_strName!=null && m_strName.trim().length()>0){
    		tvPlayDeviceID.setText( getString(R.string.Notification_Playing_Chn)+" "+m_strName);
    	}else{
    		tvPlayDeviceID.setText( getString(R.string.Notification_Playing_Chn)+" "+m_strName);
    	}
    
    	glViews[Player.CurrentSelPlayer()].setnServerID(deviceParam.getnDeviceID());
    	 
    	if(Player.CurrentSelPlayer() < 4)
		{
    		Player.setPlaying(Player.CurrentSelPlayer(), true);
    		Player.ClearGLESScreen(glViews,false,Player.CurrentSelPlayer());
    		glViews[Player.CurrentSelPlayer()].EnableRender();
    		glViews[Player.CurrentSelPlayer()].StartPlay(0, 0, mStreamType, mPlaySound, deviceParam);
    		glViews[Player.CurrentSelPlayer()].setReverse(mIsReverse);//add by luo 20141219
			glViews[Player.CurrentSelPlayer()].playAudio();
		} 

    	ptzTimerThreadID++;
    	if(m_bPTZ){
    		new PTZTimerThread(ptzTimerThreadID).start();
    	}
 
 		mIsPlaying = true;
    }
    

    @SuppressWarnings("deprecation")
	private void setQLViewVisible(boolean isVisible){ //��ʾ����
    	if(isVisible){
    		layoutImageQL.setVisibility(View.VISIBLE);
    		mBtnImageQl.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_definition_2));
    		
    		controlPanel.setVisibility(View.GONE);  //add by mai 2015-3-14
    	}else{
    		layoutImageQL.setVisibility(View.GONE);
    		mBtnImageQl.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_definition_1));
     
    		if(nScreenOrientation == Configuration.ORIENTATION_LANDSCAPE){
    			controlPanel.setVisibility(View.GONE);  
    		}else{
    			controlPanel.setVisibility(View.VISIBLE);  
    		}
    	}
    }
    
    //add by luo 20150820
    private void onStreamTypeChange(int nType){
    	if(mStreamType==nType)return;
    	
    	mStreamType = nType;

    	if(mStreamType==0){
    		
    		btnSmooth.setTextColor(Color.BLUE);
    		btnHD.setTextColor(Color.BLACK);
    		mBtnSmooth.setChecked(true);  
    	}else if(mStreamType==1){
    		btnSmooth.setTextColor(Color.BLACK);
    	    btnHD.setTextColor(Color.BLUE);
    		mBtnHD.setChecked(true);
    	} 
    	
    	if(mIsPlaying){
      	  stopPlay(false);
	      	  try {
					Thread.sleep(200);
	      	  } catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
	      	  }
	 
	      startPlay();
    	}
    }
	
    @SuppressWarnings("deprecation")
	private void onSoundChange(){  //�Ƿ�����Ƶ

		  if(mPlaySound){
	     		mBtnSound.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_back_sound_btn));
	     		mBtnSound2.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_back_sound_btn));
	    	}else{
	    		mBtnSound.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_back_sound_3));
	    		mBtnSound2.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_back_sound_3));
	    	}
		
		 //to do 
		  int nChn = Player.CurrentSelPlayer();
	      if(nChn>=0 && nChn<4){
	    	glViews[Player.CurrentSelPlayer()].SetAudioParam(mPlaySound);
	    	
	      }
		  
		  writeSystemParam(); //���浱ǰ����
    }
    
    //����¼�
    @Override
 	public void onClick(View v) {
		// TODO Auto-generated method stub
	  nToolsViewShowTickCount=5;
	  
	 if(v==btnCancelImageView){ 
		 iamgeViewDialog.dismiss();
		 return;
	 }
		
	// add by mai 2015-7-30 ���Ԥ��λ��ť
	 if(v == btnPresetConfig)
	 {
		 if(m_bPTZX)
		 {
			 presetPopuwindow(true);
		 }else{
			 Toast.makeText(getApplicationContext(), "���豸��Ԥ��λ", Toast.LENGTH_SHORT).show();
		 }
		
	 }
	 
	 if(v == ivPresetLandscape)
	 { //�������ʱ��Ԥ��λ��ť
		 if(m_bPTZX)
		 {
			 presetPopuwindow(false);
		 }else{
			 Toast.makeText(getApplicationContext(), "���豸��Ԥ��λ", Toast.LENGTH_SHORT).show();
		 }
	 }
	 
	 // end add by mai 2015-7-30
 	  if(v==mBtnScreenShot){ //��Ƶ
		  if(mIsPlaying){
			  screenshotDialog.show();
		  }
		  return;
	  }else if(v==mBtnScreenShot2){ // add by mai 2015-3-25 //��Ƶ
		  if(mIsPlaying){
			  screenshotDialog.show();
 
		  }
		  return;
	  }else if(v==mBtnReverse){//ͼ���� //add by luo 20141124
		  //modify by luo 20150106
			 if(m_bReversePRI){
				 glViews[Player.CurrentSelPlayer()].SetCamImageOrientation(Defines.NV_IPC_ORIENTATION_REVERSE);
			 }else{
				 mIsReverse=!mIsReverse;//add  by luo 20141124
				  glViews[Player.CurrentSelPlayer()].setReverse(mIsReverse);
			 }
			//end modify by luo 20150106  
	 
			 
			return;
		  
	  }else if(v==mBtnReverse2){//ͼ���� //add by mai 2015-3-25
		  //modify by luo 20150106
			 if(m_bReversePRI){
				 glViews[Player.CurrentSelPlayer()].SetCamImageOrientation(Defines.NV_IPC_ORIENTATION_REVERSE);
			 }else{
				 mIsReverse=!mIsReverse;//add  by luo 20141124
				  glViews[Player.CurrentSelPlayer()].setReverse(mIsReverse);
			 }
			//end modify by luo 20150106  
	 
			return;
		  
	  }else  // end add by mai 2015-3-25
	  if(v==mBtnImageQl){//��ʾ���ఴť�����¼�
 
		  
		mPTZPanel.setVisibility(View.GONE);
		//setMicVisible(false);
 		if(layoutImageQL.getVisibility()!=View.VISIBLE){
			setQLViewVisible(true);
		}else{
			setQLViewVisible(false);
 		}
		
		return;

	  }
     
  

	  setQLViewVisible(false);
	 
	  if(v==mBtnHD){ //�ֱ���
		  onStreamTypeChange(1);
	  }else	 if(v==mBtnSmooth){
		  onStreamTypeChange(0);
		  
	  }else if(v==btnHD){  // add by mai 2015-3-25
		  
		  onStreamTypeChange(1);
		  
	  }else	 if(v==btnSmooth){
		  
		  onStreamTypeChange(0);
	  }else	 // add by mai 2015-3-25
		  if(v==mBtnSound){ //�Խ�

		  v.setEnabled(false);
		  
		  if(mPlaySound)
		  {
			  mPlaySound = false;
		  }else{
			  mPlaySound = true;
		  }
		  onSoundChange();
		  v.setEnabled(true);
	  }else if(v==mBtnSound2){  // add by mai 2015-3-25
		  v.setEnabled(false);
		  if(mPlaySound)
		  {
			  mPlaySound = false;
		  }else{
			  mPlaySound = true;
		  }
		  onSoundChange();
		  v.setEnabled(true);
			
	  }else  // end add by mai 2015-3-25
		  if(v==mBtnStopAndPlay){  //��ʼ/ֹͣ����

		  mIsPlaying = !mIsPlaying;
		  if(mIsPlaying){
			  mPlayingChn=1;
			  startPlay();
		  }else{
			  stopPlay(true);
		  }
			    
	  }else if(v==mBtnBack){ //���ذ�ť
		  
		  if(mIsPlaying==false){
			  stopPlay(false);
 		      Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
		      m_bFinish = true;
		      startActivity(intent);
			  NVPlayerPlayActivity.this.finish();  
				
			  overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			  return;
		  }
		  new AlertDialog.Builder(NVPlayerPlayActivity.this)
			.setTitle(getString(R.string.alert_stop_play))
			.setIcon(R.drawable.icon)
			.setNegativeButton(getString(R.string.alert_btn_NO), null)
			.setPositiveButton(getString(R.string.alert_btn_YES), new DialogInterface.OnClickListener()  {

			public void onClick(DialogInterface dialog, int whichButton) {
              
			  try{
            	  stopPlay(false);
              }catch(Exception e){
            	  
              }
			  
			  setResult(RESULT_OK);
			  
			  mIsPlaying = false;
			  Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
		      m_bFinish = true;
		      startActivity(intent);
		      LocalDefines.B_UPDATE_LISTVIEW = true; // add by mai 2015-6-29 �������
			  NVPlayerPlayActivity.this.finish();  
			  
			  overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			}

			}).show();

	  }else if(v==mBtnBack2){  // add by mai 2015-3-25
		  
		  if(mIsPlaying==false){
			  stopPlay(false);
 		      Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
		      m_bFinish = true;
		      startActivity(intent);
			  NVPlayerPlayActivity.this.finish();  
				
			  overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			  return;
		  }
		  new AlertDialog.Builder(NVPlayerPlayActivity.this)
			.setTitle(getString(R.string.alert_stop_play))
			.setIcon(R.drawable.icon)
			.setNegativeButton(getString(R.string.alert_btn_NO), null)
			.setPositiveButton(getString(R.string.alert_btn_YES), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
              
			  try{
            	  stopPlay(false);
              }catch(Exception e){
            	  
              }
			  
			  setResult(RESULT_OK);
			  
			  mIsPlaying = false;
			  Intent intent = new Intent(NVPlayerPlayActivity.this, DeviceListViewActivity.class);
		      m_bFinish = true;
		      startActivity(intent);
			  NVPlayerPlayActivity.this.finish();  
			  overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			}

			}).show();

	  }  // end add by mai 2015-3-25
	 
	}

	 //������ʾ����
  	public void ShowNotic(String title, String msg){//add by luo 20141010
	  		
  		Toast toast = Toast.makeText(getApplicationContext(),
  				title, Toast.LENGTH_SHORT);
	    toast.setGravity(Gravity.CENTER, 0, 0);
			   toast.show();

  	}
  	
  	
  	/**
  	 * ��ʾ�豸�Ƿ���ҡͷ��
  	 */
  	public boolean hasPTZXPRI()
  	{
   		if(!m_bPTZ)   
		{
			ShowNotic(getString(R.string.deviceTurn),"");
			btnPTZDown.setEnabled(false);
			btnPTZLeft.setEnabled(false);
			btnPTZRight.setEnabled(false);
			btnPTZUP.setEnabled(false);
		}
		
  		return m_bPTZ;
  	}
  	

	//�������ť
	@SuppressWarnings("deprecation")
	@Override
	public boolean onTouch(View v, MotionEvent event) {//
	
		
 	    if(v==this.layoutCenter){
 	    	mScaleGestureDetector.onTouchEvent(event);
 	    	
 	    	if(System.currentTimeMillis() - lScaleTime>500){
 	    		mGestureDetector.onTouchEvent(event);
 	    	}
	    	
	    	
	    	return true;
	    }else  if(v==this.mBtnMic){
 	    		
	    		if(!mIsPlaying){
	    			return true;
	    		}

	    		switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						
						mBtnMic.setBackgroundResource(R.drawable.play_talkback_2);
						llPlayTalkback.setVisibility(View.VISIBLE);
						mIsSpeaking = true;
						
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						glViews[Player.CurrentSelPlayer()].StartSpeak();

						break;
					case MotionEvent.ACTION_CANCEL:
						
						mBtnMic.setBackgroundResource(R.drawable.play_talkback_1);
						llPlayTalkback.setVisibility(View.GONE);
						mIsSpeaking = false;

						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
						glViews[Player.CurrentSelPlayer()].StopSpeak();

						break;
					
					case MotionEvent.ACTION_MOVE: 
						mIsSpeaking = true;
						mBtnMic.setBackgroundResource(R.drawable.play_talkback_2);
						llPlayTalkback.setVisibility(View.VISIBLE);
						break;
					case MotionEvent.ACTION_UP:
						mBtnMic.setBackgroundResource(R.drawable.play_talkback_1);
						llPlayTalkback.setVisibility(View.GONE);
						mIsSpeaking = false;
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					glViews[Player.CurrentSelPlayer()].StopSpeak();
						 
					break;
					default:break;
				}
	    		
	    		
				return true;
	    }else if(v==this.mBtnMic2 ){  // add by mai 2015-3-25
    		
    		if(!mIsPlaying){
    			return true;
    		}
    		
    		switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN:

					mBtnMic2.setBackgroundResource(R.drawable.play_talkback_2);
					llPlayTalkback.setVisibility(View.VISIBLE);
					mIsSpeaking = true;
					
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					glViews[Player.CurrentSelPlayer()].StartSpeak();
					
					
					break;
				case MotionEvent.ACTION_CANCEL:

					mBtnMic2.setBackgroundResource(R.drawable.play_talkback_1);
					llPlayTalkback.setVisibility(View.GONE);
					mIsSpeaking = false;

					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					glViews[Player.CurrentSelPlayer()].StopSpeak();
					break;
				
				case MotionEvent.ACTION_MOVE: 
					mIsSpeaking = true;
					mBtnMic2.setBackgroundResource(R.drawable.play_talkback_2);
					llPlayTalkback.setVisibility(View.VISIBLE);

					break;
				case MotionEvent.ACTION_UP:
					mBtnMic2.setBackgroundResource(R.drawable.play_talkback_1);
					llPlayTalkback.setVisibility(View.GONE);
					mIsSpeaking = false;
					
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					glViews[Player.CurrentSelPlayer()].StopSpeak();
					 
					break;
				default:break;
			}
    		
    		
			return true;
	    }else if(v == this.btnPTZLeft)  //���������߰�ť,
	    {
    		//�豸�Ƿ���ҡͷ��
    		if(!hasPTZXPRI()){
    			return true;
    		}
    		
	    	switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					if(m_bPTZ)
					{
						 btnPTZLeft.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_left_2))); //add by mai 2015-7-9 ��̬��ӱ���
					}
					 bIsLeftPressed=true;

					break;
					
				case MotionEvent.ACTION_CANCEL:
					 btnPTZLeft.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_left_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsLeftPressed=false;
					
					break;
				
				case MotionEvent.ACTION_MOVE: 
 
					break;
				case MotionEvent.ACTION_UP:
					 btnPTZLeft.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_left_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsLeftPressed=false;
					break;
				default:break;
			}
	    	
	    }else if(v == this.btnPTZRight)  //��������ұ߰�ť
	    {
	    	//�豸�Ƿ���ҡͷ��
    		if(!hasPTZXPRI()){
    			return true;
    		}
    		
	    	switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					if(m_bPTZ)
					{
						btnPTZRight.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_right_2))); //add by mai 2015-7-9 ��̬��ӱ���
					}
					 bIsRightPressed=true;
					 
					break;
					
				case MotionEvent.ACTION_CANCEL:
					btnPTZRight.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_right_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsRightPressed=false;
					
					break;
				
				case MotionEvent.ACTION_MOVE: 
 
					break;
				case MotionEvent.ACTION_UP:
					btnPTZRight.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_right_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsRightPressed=false;
					break;
				default:break;
			}
	    	
	    }else if(v == this.btnPTZUP)  //��������ϱ߱߰�ť
	    {
	    	//�豸�Ƿ���ҡͷ��
    		if(!hasPTZXPRI()){
    			return true;
    		}
    		
	    	switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					if(m_bPTZ)
					{
						 btnPTZUP.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_top_2))); //add by mai 2015-7-9 ��̬��ӱ���
					}
					bIsUpPressed=true;
					
					break;
					
				case MotionEvent.ACTION_CANCEL:

					 btnPTZUP.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_top_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsUpPressed=false;
					break;
				
				case MotionEvent.ACTION_MOVE: 
 
					break;
				case MotionEvent.ACTION_UP:
					
					 btnPTZUP.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_top_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsUpPressed=false;
					break;
				default:break;
			}
	    	
	    }else if(v == this.btnPTZDown)  //��������±߰�ť
	    {
	    	//�豸�Ƿ���ҡͷ��
    		if(!hasPTZXPRI()){
    			return true;
    		}
    		
	    	switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					if(m_bPTZ)
					{
						btnPTZDown.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_bottom_2))); //add by mai 2015-7-9 ��̬��ӱ���
					}
					bIsDownPressed=true;
					break;
					
				case MotionEvent.ACTION_CANCEL:
					
					btnPTZDown.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_bottom_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsDownPressed=false;
					break;
				
				case MotionEvent.ACTION_MOVE: 
					
					break;
				case MotionEvent.ACTION_UP:
					btnPTZDown.setBackgroundDrawable(new BitmapDrawable(Functions.readBitMap(this, R.drawable.play_bottom_1))); //add by mai 2015-7-9 ��̬��ӱ���
					bIsDownPressed=false;
					break;
				default:break;
			}
	    	
	    }
	    return false;
	 
	}
	
 
	private void showToolsViews(){//��ʾ����������
		System.out.println("showToolsViews");//add for test
		if(popupWindowMore !=null)
		{
			popupWindowMore.dismiss();
		}
		nToolsViewShowTickCount=5;
		layoutBottomBar.setVisibility(View.VISIBLE);
		if(bAnyway)
		{
			layoutTopBar.setVisibility(View.VISIBLE);
			llVertical.setVisibility(View.VISIBLE);
			rgLandscapeDefinition.setVisibility(View.VISIBLE);
			ivPresetLandscape.setVisibility(View.GONE);  //add  by mai 2015-7-31 ����ʱ����
			llLandscape.setVisibility(View.GONE);
			llLandscapeDefinition.setVisibility(View.GONE);
			
			 
		}else{
			layoutTopBar.setVisibility(View.GONE);
			llVertical.setVisibility(View.GONE);
			rgLandscapeDefinition.setVisibility(View.GONE);
			llLandscape.setVisibility(View.VISIBLE);
			llLandscapeDefinition.setVisibility(View.VISIBLE);
			
			 if(m_bPTZX)
			 {
				 ivPresetLandscape.setVisibility(View.VISIBLE);
			 }else{
				 ivPresetLandscape.setVisibility(View.INVISIBLE);
			 }
		}
		
		LayoutMicroPhoneBottom.setVisibility(layoutBottomBar.getVisibility());
		 
	}
	
	private void hideToolsViews(){ //Ӱ�ز���������
		
		nToolsViewShowTickCount=0;
		layoutBottomBar.setVisibility(View.GONE);
		layoutTopBar.setVisibility(View.GONE);
		mPTZPanel.setVisibility(View.GONE);
		llLandscape.setVisibility(View.GONE);
		llLandscapeDefinition.setVisibility(View.GONE);
		ivPresetLandscape.setVisibility(View.GONE);  // add by mai 2015-7-31
		setQLViewVisible(false);
		LayoutMicroPhoneBottom.setVisibility(layoutBottomBar.getVisibility());
	}
	
	///timer task
	//����ʱ��ʱӰ�ز���������
	private int nToolsViewShowTickCount = 8;
	
	private int timerThreadID=0;
	
	class TimerThread extends Thread{
		int mThreadID=0;
		 public TimerThread(int nThreadID){
			 mThreadID = nThreadID;
		 }
		 public void run(){
			 while(mThreadID == timerThreadID){
				 
				 nToolsViewShowTickCount-=1;
		    	  if(nToolsViewShowTickCount<=0 && mThreadID == timerThreadID){
			    	  Message message = new Message();      
			    	  message.arg1 = Defines.MSG_HIDE_TOOLVIEW;     
			    	  handler.sendMessage(message); 
		    		  nToolsViewShowTickCount=0;
		    	  } 
		    	  
		    	 try {
					sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 }
			
		 }
	}

	
	private float fScaleSize = 1.0f;
	private long lScaleTime = 0;
	class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener {//���ӷŴ�����

		@Override
		public boolean onScale(ScaleGestureDetector detector) 
		{
			if(detector.getScaleFactor()>1){
				fScaleSize = fScaleSize-0.008f;
				if(fScaleSize<0.2)
				{
					fScaleSize = 0.2f;
				}else{
					glViews[Player.CurrentSelPlayer()].scale(fScaleSize, fScaleSize); 
				}
	            	  
			}else if(detector.getScaleFactor()<1)
			{
				fScaleSize = fScaleSize+0.05f;
				if(fScaleSize>1)
				{
					fScaleSize = 1.0f;
				}else{
					glViews[Player.CurrentSelPlayer()].scale(fScaleSize, fScaleSize); 
				}
			}
			
	        lScaleTime = System.currentTimeMillis();
//	             
	      return false;
		}
	  
	    @Override
	    public boolean onScaleBegin(ScaleGestureDetector detector) 
	    {
	        return true;
	    }
	 
	    @Override
	    public void onScaleEnd(ScaleGestureDetector detector) 
	    {

	    }
	    
	      
	 }
	
	private int ptzTimerThreadID=0;
	class PTZTimerThread extends Thread{//��̨���ư��������߳�
		int mThreadID=0;
		public PTZTimerThread(int nThreadID)
		{
			 mThreadID = nThreadID;
		}
		
		public void run(){
			 boolean bLeft=false, bRight=false, bUp=false, bDown=false;
			 while(mThreadID == ptzTimerThreadID)
			 {
				 bLeft=bIsLeftPressed;
				 bRight=bIsRightPressed;
				 bUp=bIsUpPressed;
				 bDown=bIsDownPressed; 
				 
				 
				 if(bLeft && bRight){
					 bLeft=false;
					 bRight=false;
				 }
				 
				 if(bUp && bDown){
					 bUp=false;
					 bDown=false;
				 }
		    	  
				 if(bLeft||bRight||bUp||bDown){
					 glViews[Player.CurrentSelPlayer()].SendPTZAction(bLeft, bRight, bUp, bDown, 0);
					 ///
					 try {
							sleep(200);
					} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}
				 }else{
					
					 try {
							sleep(50);
					} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}
				 }
		    	
			 }
			
		 }
	}
	//OnGestureListener 	
	class PTZGestureListener extends SimpleOnGestureListener {//��̨�������Ƽ���
		  
		 public static final int MOVE_TO_LEFT = 0;  
		 public static final int MOVE_TO_RIGHT =1;  
		 public static final int MOVE_TO_UP = 2;  
		 public static final int MOVE_TO_DOWN = 3;  
		 private int nStep = 0;  
		  
		 boolean bTouchLeft=false, bTouchRight=false,bTouchUp=false,bTouchDown=false;
         double nVelocityX = 0;//ˮƽ�ƶ����ٶ�
         double nMoveDistanceX = 0;//ˮƽ�ƶ��ľ���
         
         double nVelocityY = 0;//��ֱ�ƶ����ٶ�
         double nMoveDistanceY = 0;//��ֱ�ƶ��ľ���
         
		 float x1 = 0;  
		 float x2 = 0;  
		 float y1 = 0;  
		 float y2 = 0;  
		    
		 PTZGestureListener(Context context) 
		 { 
		 }
 
	     @Override  
	     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
	                float distanceY) 
	     {  
 
	         return false;  
	     }  
	      
	         
	     @Override  
	     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
	            float velocityY) {  

	        	if(bIsLeftPressed || bIsRightPressed || bIsUpPressed || bIsDownPressed){
	        		return true;
	        	}
	            bTouchLeft=false;
	            bTouchRight=false;
	            bTouchUp=false;
	            bTouchDown=false;
	            nVelocityX = Math.abs(velocityX);//ˮƽ�ƶ����ٶ�
	            nMoveDistanceX = Math.abs(e1.getX() - e2.getX());//ˮƽ�ƶ��ľ���
	            nVelocityY = Math.abs(velocityY);//��ֱ�ƶ����ٶ�
	            nMoveDistanceY = Math.abs(e1.getY() - e2.getY());//��ֱ�ƶ��ľ���
	            
	            if(nVelocityY>=nVelocityX){
	            	nVelocityX =0;
            	}else{
            		nVelocityY=0;
            	}

	            if(nVelocityY < nVelocityX){
	            	nStep = 0;
	            	
	            	if(nMoveDistanceX>FLING_MIN_DISTANCE){
	            		nStep=1;
	            		if(nMoveDistanceX>FLING_MAX_DISTANCE){
	            			nStep = (int) (nMoveDistanceX/FLING_MAX_DISTANCE);
	            		}
	            	}
	            	if(nVelocityX > FLING_MIN_VELOCITY && nMoveDistanceX > FLING_MIN_DISTANCE){
		            	if(e1.getX()>e2.getX()){
		            		//@@System.out.println("onFling: Left");
		            		
		            		bTouchLeft=true;
		            	}else{
		            		//@@System.out.println("onFling: Right");
		            		bTouchRight=true;
		            	}
		            }
	            	
            	}else if(nVelocityY > nVelocityX){

            		nStep = 0;
	            	
	            	if(nMoveDistanceX>FLING_MIN_DISTANCE){
	            		nStep=1;
	            		if(nMoveDistanceX>FLING_MAX_DISTANCE){
	            			nStep = (int) (nMoveDistanceX/FLING_MAX_DISTANCE);
	            		}
	            	}
            		
            		if(nVelocityY > FLING_MIN_VELOCITY && nMoveDistanceY > FLING_MIN_DISTANCE){
    	            	if(e1.getY()>e2.getY()){
    	            		//@@System.out.println("onFling: UP");
    	            		bTouchUp=true;
    	            	}else{
    	            		//@@System.out.println("onFling: Down");
    	            		bTouchDown=true;
    	            	}
    	            	
    	            }
            	}else{
            		
            		if(nMoveDistanceY>=nMoveDistanceX){
            			if(nVelocityY > FLING_MIN_VELOCITY && nMoveDistanceY > FLING_MIN_DISTANCE){
        	            	if(e1.getY()>e2.getY()){
        	            		//@@System.out.println("onFling: UP");
        	            		bTouchUp=true;
        	            	}else{
        	            		//@@System.out.println("onFling: Down");
        	            		bTouchDown=true;
        	            	}
        	            	
        	            }
                	}else{
                		if(nVelocityX > FLING_MIN_VELOCITY && nMoveDistanceX > FLING_MIN_DISTANCE){
    		            	if(e1.getX()>e2.getX()){
    		            		//@@System.out.println("onFling: Left");
    		            		bTouchLeft=true;
    		            	}else{
    		            		//@@System.out.println("onFling: Right");
    		            		bTouchRight=true;
    		            	}
    		            }
                	}
    	            
            	}
	            
	            if(nStep>5){
	            	nStep=5;
	            }
	            //@@System.out.println("onFling: "+bTouchLeft+", "+bTouchRight+", "+bTouchUp+", "+bTouchDown+" "+ nStep);
	            
	            glViews[Player.CurrentSelPlayer()].SendPTZAction(bTouchLeft, bTouchRight, bTouchUp, bTouchDown, nStep);
 
	            return false;  
	        }  

	        /**
	         * ���������ͬ��onSingleTapUp��������GestureDetectorȷ���û��ڵ�һ�δ�����Ļ��û�н����ŵڶ��δ�����Ļ��Ҳ���ǲ��ǡ�˫������ʱ�򴥷�
	         * */
	        @Override
	        public boolean onSingleTapConfirmed(MotionEvent e) {
	        	Log.i("a", "onSingleTapConfirmed"); 
	        	
	        	if(layoutBottomBar.getVisibility()==View.VISIBLE){
 	        		
					if(layoutImageQL.getVisibility()!=View.VISIBLE && nScreenOrientation == Configuration.ORIENTATION_LANDSCAPE){

 						hideToolsViews();
					}
					 
				}else{
 					
					showToolsViews();
				}
	            return false;
	        }
	 
		  
		}
	
 
	/**
	 * add by mai 2015-7-30
	 * Ԥ��λ����
	 * 
	 */
	private void startPTZXConfig(LoginHandle lhandle,int nPTZXID, int nPTZXAction)//Ԥ��λ��λ
	{
		m_nPTZXID++;
		new PTZXThread(handler, m_nPTZXID, lhandle, nPTZXID, nPTZXAction).start();
		
	}
	
	private void locationPTZXPoint(int nPTZXID){//Ԥ��λλ���趨
		
		glViews[Player.CurrentSelPlayer()].CallPTZXLocationID(nPTZXID, deviceParam);
	}
	
	/**
	 *add by mai 2015-7-30
	 * Ԥ��λ����ʹ���߳�
	 * @author Administrator
	 *
	 */
	
	private int m_nPTZXID = 0;
    private class PTZXThread extends Thread{//Ԥ��λ�����߳�
    	
    	private Handler handler;
    	private int m_ThreadConfigID=0;
    	LoginHandle lhandle=null;
    
    	
    	
    	private int m_ThreadPTZXID = 0;  //Ԥ��λID
    	private int m_ThreadPTZXAction = 0; // Ԥ��λ����
    	


    	//��ȡ�������ù��캯��
    	public PTZXThread(Handler handler,int configID,LoginHandle lhandle, int nPTZXID, int nPTZXAction){
    		m_ThreadConfigID = configID;
    		this.handler=handler;
    		this.lhandle = lhandle;
    		this.m_ThreadPTZXID = nPTZXID;
    		this.m_ThreadPTZXAction = nPTZXAction;
    	}
    	
    	
        
       
    	
        public void run(){
    		
    			int nConfigResult = glViews[Player.CurrentSelPlayer()].SetPTZXLocationID(m_ThreadPTZXID, lhandle);  // 
    			if(m_nPTZXID==m_ThreadConfigID && nConfigResult==ResultCode.RESULT_CODE_SUCCESS){//�������ò��ɹ�
    				Message msg=handler.obtainMessage();
	      			msg.arg1= PTZX_RESULT_OK;
	  				msg.arg2 = m_ThreadPTZXID;
	  				
	  				handler.sendMessage(msg);
				}else if(m_nPTZXID==m_ThreadConfigID){
					Message msg=handler.obtainMessage();
     				msg.arg1=PTZX_RESULT_FAIL;
     				msg.arg2 = nConfigResult;
     				handler.sendMessage(msg);
				}
    			
    			 
    		}
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
    
}

 
